package com.boco.eoms.report.statistic.manager;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.boco.eoms.report.common.Parameter;
import com.boco.eoms.report.common.Report;
import com.boco.eoms.report.common.ReportsBO;
import com.boco.eoms.report.statistic.util.GenXml;


public class StatisticManager {
	private Log logger=LogFactory.getLog(this.getClass().getName());
	private String fileName="";
	/**
	 * 系统初次运行时生成所有的report文件,可以在系统初次启动时调用
	 * @author lzp
	 */
	public void init(){
		logger.info("Flash report Init start");
		String[] fileName=this.fileName.split(",");
		ReportsBO bo=ReportsBO.getInstance();
		Map reports=bo.getReports();
		Set reportSet=reports.keySet();
		List<Report> fileList=new ArrayList();
		for(Iterator iterator=reportSet.iterator();iterator.hasNext();){
			Report report=(Report)reports.get(iterator.next().toString());
			Parameter[] parameters=report.getReportInfo().getParameters();
			for(int i=0;i<fileName.length;i++){
				if(fileName[i].equals(parameters[0].getValue())){
					fileList.add(report);
					break;
				}
			}
		}
		String path=bo.getRealPath();
		GenXml.genPortletObjectRef(fileList, path);
		logger.info("Flash report Init end");
	}
	/**
	 * 获取用于存储统计数据的XML文件名称
	 * @author lzp
	 */
	private void getFileName(){
		String fileName="";
		Properties properties=new Properties();
			try{
				properties.load(this.getClass().getResourceAsStream("/config/tool.properties"));
			}catch(IOException e){
				this.logger.error(e);
			}
			
		Collection collection=properties.values();
		int i=0;
  		for(Iterator iterator=collection.iterator();iterator.hasNext();i++){
			if(i!=0){
				fileName=fileName+",";
			}
			fileName=fileName+iterator.next().toString().trim();	
		}
//   		logger.info(fileName);
   		this.fileName=fileName;
	}
	/**
	 * 非实时生成统计图表
	 * @author lzp
	 */
	public synchronized void  doAuth(){
		logger.info("Test Start!");
		this.getFileName();
		String[] fileName=this.fileName.split(",");
		ReportsBO bo=ReportsBO.getInstance();
		bo.refresh();
		Map reports=bo.getReports();
		Set reportSet=reports.keySet();
		List<Report> fileList=new ArrayList();
		for(Iterator iterator=reportSet.iterator();iterator.hasNext();){
			Report report=(Report)reports.get(iterator.next().toString());

			Parameter[] parameters=report.getReportInfo().getParameters();
			if("Flash".equalsIgnoreCase(parameters[parameters.length-1].getValue())){				

			    for(int i=0;i<fileName.length;i++){
				    if((fileName[i].equals(parameters[0].getValue()))&&!report.getReportInfo().isRealtime()){

					    fileList.add(report);
					    break;
				    }
			    }
			}
		}

		String path=bo.getRealPath();
		GenXml.genPortletObjectRef(fileList,path);
		logger.info("Test end!");

	}
	/**
	 * 适时生成数据统计文件
	 * @author lzp
	 */
	public synchronized void  doRealtime(){
		logger.info("Test start!");
//		this.getFileName();
//		String[] fileName=this.fileName.split(",");
//		ReportsBO bo=ReportsBO.getInstance();
//		bo.refresh();
//
//		Map reports=bo.getReports();
//		Set reportSet=reports.keySet();
//		List<Report> fileList=new ArrayList();
//		for(Iterator iterator=reportSet.iterator();iterator.hasNext();){
//			Report report=(Report)reports.get(iterator.next().toString());
//			Parameter[] parameters=report.getReportInfo().getParameters();
//			if("Flash".equalsIgnoreCase(parameters[parameters.length-1].getValue())){
//                
//				for(int i=0;i<fileName.length;i++){
//					if((fileName[i].equals(parameters[0].getValue()))&&report.getReportInfo().isRealtime()){
//
//						fileList.add(report);
//						break;
//					}
//				}				
//			}
//		}
//
//		String path=bo.getRealPath();
//		GenXml.genPortletObjectRef(fileList,path);
		
		ReportsBO bo=ReportsBO.getInstance();
		bo.refresh();
		Map reports=bo.getReports();
		Set reportSet=reports.keySet();
		List<Report> fileList=new ArrayList();
		for(Iterator iterator=reportSet.iterator();iterator.hasNext();){
			Report report=(Report)reports.get(iterator.next().toString());
			Parameter[] parameters=report.getReportInfo().getParameters();
			if("Flash".equalsIgnoreCase(parameters[parameters.length-1].getValue())){
				if(report.getReportInfo().isRealtime()){
					fileList.add(report);
				}
			}
		}
		String path=bo.getRealPath();
		logger.info(path);
		GenXml.genNopStatisticXml(fileList,path);
		logger.info("Test end!");		
	}
		
}
